import { reqGetDetailInfoData } from "@/api";

export default {
  namespaced: true,
  state: {
    detailInfoData: {}
  },
  actions: {
    // 请求数据
    async getDetailInfoData({ commit }, id) {
      let res = await reqGetDetailInfoData(id);
      if (res.code === 200) {
        commit('SAVED_DETAIL_INFO_DATA', res.data)
      } else {
        console.log(res.message);
      }
    }
  },
  mutations: {
    SAVED_DETAIL_INFO_DATA(state, value) {
      state.detailInfoData = value
    },
    // 放大镜下面的图片切换时更改图片
    CHANGE_SKUINFO_IMGURL(state, skuImg) {
      // 1、首先将小图的isDefault属性改为'0'
      state.detailInfoData.skuInfo.skuImageList.forEach(item => {
        if (item.id === skuImg.id) {
          // 2、其次将被点击的图片显示高亮（即将isDefault属性改为'1'）
          item.isDefault = '1'
          // 3、最后更改大图的路径
          state.detailInfoData.skuInfo.skuDefaultImg = item.imgUrl;
        } else {
          item.isDefault = '0'
        }
      });

    }
  },
  getters: {
    // 解构detailInfoData数据
    categoryView(state) {
      return state.detailInfoData.categoryView || {}
    },
    skuInfo(state) {
      return state.detailInfoData.skuInfo || {}
    },
    spuSaleAttrList(state) {
      return state.detailInfoData.spuSaleAttrList || []
    }
  }
}